<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/6/12
 * Time: 15:14
 */

namespace app\api\controller;

use think\controller;
use think\Validate;
use think\Request;
use think\Db;
class Device extends Auto
{

    //查询 设备 机构
    protected $dev_rule =   [
        'id'=>'require',
        'org_code'  => 'require|max:25',
        'pageindex'  => 'number',
        'pagesize'  => 'number',
    ];
//查询 设备 机构
    protected $dev_message  =   [
        'id.require' => 'id必填参数',
        'org_code.require' => 'org_code必填参数',
        'org_code.max'     => '用户名最多不能超过25个字符',
        'pageindex.number'     => 'pageindex为int型',
        'pagesize.number'     => 'pagesize为int型',
    ];

//查询 设备 设备编码
    protected $dev_no_rule =   [
        'id'=>'require',
        'dev_code'  => 'require|max:25',
        'org_code'=>'require',

    ];
//查询 设备 设备编码
    protected $dev_no_message  =   [
        'id.require' => 'id必填参数',
        'dev_code.require' => 'dev_code必填参数',
        'org_code.require' => 'org_code必填参数',

    ];
    /**
     * @desc 通过机构编码查询设备
     * @return array
     */
    public function devquerybyorg(){
        try{
            $data = [
                'id'=>input('id'),
                'org_code'=>input('org_code'),
                'pageindex'=>input('pageindex')?input('pageindex'):1,
                'pagesize'=>input('pagesize')?input('pagesize'):10,
            ];
            $validate = new Validate($this->dev_rule,$this->dev_message);
            $result   = $validate->check($data);
            if(!$result){
                $message = $validate->getError();
                return json(returnRest($message,[],100001));
            }
            $pageindex = ($data['pageindex']-1)*10;
            $pagesize = $data['pagesize'];
            $pagestr = $pageindex.','.$pagesize;
            unset($data['id']);
            unset($data['pageindex']);
            unset($data['pagesize']);
            $result  = Db::table('device')->field('dev_sn,dev_color,dev_code,dev_name,dev_mac')->where($data)->limit($pagestr)->select();
            $resultcount  = Db::table('device')->where($data)->count();
            $message ='';
            $content = [
                'pageindex'=>$pageindex+1,
                'pagesize'=>$pagesize,
                'pagecount'=>$resultcount,
                'content'=>$result
            ];
            return json(returnRest($message,$content,0));
        } catch (\Exception $e){
            $message= $e->getMessage();
            return json(returnRest($message,[],9000));
        }
    }

    /**
     * 通过设备编码查询设备
     * @return \think\response\Json
     */
    public function devquerybyno(){

        try{
            $data = [
                'id'=>input('id'),
                'org_code'=>input('org_code'),
                'dev_code'=>input('dev_code'),
            ];
            $validate = new Validate($this->dev_no_rule,$this->dev_no_message);
            $result   = $validate->check($data);
            if(!$result){
                $message = $validate->getError();
                return json(returnRest($message,[],100001));
            }
            unset($data['id']);
            $result  = Db::table('device')->field('dev_sn,dev_color,dev_code,dev_name,dev_mac')->where($data)->select();
            $message ='';
            $content = [
                'content'=>$result
            ];
            return json(returnRest($message,$content,0));


        } catch (\Exception $e){
            $message= $e->getMessage();
            return json(returnRest($message,[],9000));
        }
    }

    /**
     * 通过设备编码查询设备
     * @return \think\response\Json
     */
    public function querybymac(){

        try{
            $data = [
                'id'=>input('id'),
            ];
            $user_rule = [
                'id' => 'require',
            ];
            $validate = new Validate($user_rule);
            $result   = $validate->check($data);
            if(!$result){
                $message = $validate->getError();
                return json(returnRest($message,[],100001));
            }
            $content =input('keyword');
            $result  = Db::table('device')->field('dev_sn,dev_color,dev_code,dev_name,dev_mac')
                ->where('dev_mac|dev_code',$content)
//                ->whereOr('dev_code',$content)
                ->select();
            $message ='';
            $content = [
                'content'=>$result
            ];
            return json(returnRest($message,$content,0));


        } catch (\Exception $e){
            $message= $e->getMessage();
            return json(returnRest($message,[],9000));
        }
    }


    /**
     * @return \think\response\Json
     */
    public function add(){
        try {

                $data = [
                 'id' => input('id'),
                 'dev_code' => input('dev_code'),
                 'dev_mac' => input('dev_mac'),
                 'dev_type' => input('dev_type'),
                 'dev_edition' => input('dev_edition'),
                 'dev_soft_edition' => input('dev_soft_edition'),
                 'org_code' => 'ORG_5a7d07831cca6',
                 'dev_color' => '#000000',
                 'dev_status' => 1,
                 'dev_name' => '设备' . uniqid(),
                 'mark' => 1,
                 'create_date' => date('Y-m-d H:i:s', time())
            ];
            //查询 设备 机构
                $user_rule = [
                    'id' => 'require',
                    'dev_code' => 'require|max:25',
                    'dev_mac' => 'require|max:25',
                    'dev_type' => 'require|max:25',
                    'dev_edition' => 'require|max:25',
                    'dev_soft_edition' => 'require|max:25',
                ];
                $user_message = [
                    'id.require' => 'id必填参数',
                    'dev_code.require' => 'dev_code必填参数',
                    'dev_code.max' => '设备名称最多不能超过25个字符',
                    'dev_mac.require' => 'dev_mac必填参数',
                    'dev_mac.max' => '设备mac最多不能超过25个字符'
                ];
                $validate = new Validate($user_rule,$user_message);
                $result = $validate->check($data);
                if (!$result) {
                    $message = $validate->getError();
                    return json(returnRest($message, [], 100001));
                }
                unset($data['id']);
                $result = Db::table('device')->insert($data);
                $message ='添加成功';
                return json(returnRest($message, [], 0));
        }catch (\Exception $e){
            $message= $e->getMessage();
            return json(returnRest($message,[],9007));
        }
    }

    public function test(){
        var_dump(input());
    }

    /**
     * 查询涉笔是否存在 是否匹配
     * @param array $paramdate
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getdevicemark(array $paramdate){

        $result = Db::table('device')
            ->where($paramdate)
            ->select();

        if(empty($result)){
            return false;
        }
        return true;

    }

}